package game.ui
{
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import game.core.GameSystem;
	import game.core.util.TextFieldUtil;
	import game.event.GameEvent;
	import game.manager.ProfileManager;
	import game.manager.XMLManager;
	

	public class Screen extends Sprite
	{
		public function Screen()
		{
			super();
		}
		/**
		 *界面背景 
		 */		
		private var backGround:Sprite = new Sprite();
		/**
		 *分数显示面板 
		 */		
		private var scoreGround:Sprite = new Sprite();
		/**
		 *时间显示面板 
		 */		
		private var timeGround:Sprite = new Sprite();
		/**
		 *开始按钮 
		 */		
		private var startBtn:Sprite = new Sprite();
		
		/**
		 *倒计时 timer 
		 */		
		private var gameTimer:Timer ;
		/**
		 *游戏时间  set by xml
		 */		
		private var gameDuration:int = 180000;
		/**
		 *剩余时间 
		 */		
		private var leftTime:int;
		
		private var startGame:Function;
		
		private var surplusTextField:TextField;
		
		public function init(startHandle:Function = null):void{
			startGame = startHandle;
			addChild(backGround);
			addChild(scoreGround);
			addChild(timeGround);
			addChild(startBtn);
			
			x = 10;
			y = 10;
			setBackground();
			setScoreUI();
			setTimeUI();
			setStartBtn();
			gameDuration = XMLManager.instance.getConfig("config",null,"duration");
			ProfileManager.instance.addEventListener(ProfileManager.SCORE_CHANGE, scoreChange);
			
			//////////////测试
			surplusTextField = new TextField();
			surplusTextField.x = 200;
			surplusTextField.y = 300;
			surplusTextField.visible = false;
			this.addChild(surplusTextField);
			configUI();
		}
		
		private var stopTextField:TextField;
		private function configUI():void {
			stopTextField = new TextField();
			this.addChild(stopTextField);
		}
		
		public function updateStopTimer(value:Number):void {
			if(value == 0){
				stopTextField.visible = false;
			}else {
				stopTextField.text = "暂时时间:" + value / 1000;
			}
		}
		
		public function updateSurplusTextField(value:Number):void {
			surplusTextField.visible = true;
			surplusTextField.text = value.toString();
		}
		
		public function start():void{
			if(gameTimer == null){
				gameTimer = new Timer(1000, gameDuration/1000);
			}
			gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timeCompleteHandle);
			gameTimer.addEventListener(TimerEvent.TIMER, timerHandle);
			gameTimer.start();
		}
		
		public function setBackground(d:DisplayObject = null):void{
			if(d == null){
				var s:Sprite = new Sprite();
				var g:Graphics = s.graphics;
				g.clear();
				g.lineStyle(2,0x000000);
				g.drawRect(0,0,740,580);
				g.endFill();
				d = s;
			}
			backGround.addChild(d);
		}
		
		public function setScoreUI(d:DisplayObject = null):void{
			if(d == null){
				var tf:TextField = TextFieldUtil.getScoreTextfield();
				tf.text = "分数：0";
				d = tf;
				d.x = width - d.width;
				d.y = 15;
					
			}
			scoreGround.addChild(d);
		}
		
		public function setTimeUI(d:DisplayObject = null):void{
			if(d == null){
				var tf:TextField = TextFieldUtil.getScoreTextfield();
				tf.text = "剩余时间：" + (gameDuration/1000).toString();;
				d = tf;
				d.x = width/2;
				d.y = 15;
			}
			leftTime = gameDuration/1000;
			timeGround.addChild(d);
		}
		
		public function setStartBtn(d:DisplayObject = null):void{
			if(d == null){
				var tf:TextField = TextFieldUtil.getScoreTextfield();
				tf.text = "开始游戏";
				d = tf;
				d.x = 40;
				d.y = 15;
				tf.mouseEnabled = false;
				startBtn.buttonMode = true;
				startBtn.useHandCursor = true;
			}
			startBtn.addEventListener(MouseEvent.CLICK, startHandle);
			startBtn.addChild(d);
		}
		
		protected function scoreChange(e:Event):void{
			setScore(ProfileManager.instance.score.toString());
		}
		
		public function setScore(s:String):void{
			(scoreGround.getChildAt(0) as TextField).text =  "分数：" + s;
		}
		
		public function setTime(s:String):void{
			leftTime = int(s);
			(timeGround.getChildAt(0) as TextField).text = "剩余时间：" + s;
		}
		
		protected function timerHandle(e:TimerEvent):void{
			setTime((leftTime - 1).toString());
		}
		
		protected function timeCompleteHandle(e:TimerEvent):void{
			//游戏时间到  抛事件出去 gamesystem处理
			gameTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, timeCompleteHandle);
			gameTimer.removeEventListener(TimerEvent.TIMER, timerHandle);
			gameTimer = null;
			var ge:GameEvent = new GameEvent(GameEvent.TIME_OVER);
			dispatchEvent(ge);
		}
		
		protected function startHandle(e:MouseEvent):void{
			if(startGame != null){
				GameSystem.instance.dispose();
				startGame();
			}
		}
	}
}